package com.tnxst.phexamination.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.tnxst.phexamination.R;
import com.tnxst.phexamination.callback.AppCallBack;
import com.tnxst.phexamination.constant.HISConfig;
import com.tnxst.phexamination.dto.DoctorDTO;
import com.tnxst.phexamination.utils.GsonUtils;
import com.tnxst.phexamination.utils.RequestUtils;
import com.tnxst.phexamination.utils.Utils;
import com.tnxst.phexamination.viewmodel.GlobalViewModel;
import com.tnxst.phexamination.vo.FollowThreeToSixYearVo;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import android.graphics.Color;
import android.widget.Toast;

import org.apache.commons.lang.StringUtils;

import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProvider;
import okhttp3.Call;
import worker8.com.github.radiogroupplus.RadioGroupPlus;

/**
 * 新生儿随访记录
 */
public class ThreeToSixYearFollowActivity extends BaseActivity implements View.OnClickListener {
    private GlobalViewModel viewModel;
    private Context context;
    private EditText textView177;
    private TextView textView179;
    private EditText textView183;
    private EditText textView184;
    private TextView textView368;

    private EditText editTextTextPersonName44;
    private RadioGroupPlus radioGroupPlus021;
    private RadioButton radioButton129;
    private RadioButton radioButton130;
    private RadioButton radioButton132;
    private EditText editTextTextPersonName46;
    private RadioGroupPlus radioGroupPlus020;
    private RadioButton radioButton207;
    private RadioButton radioButton208;
    private RadioButton radioButton209;

    private RadioGroupPlus radioGroupPlus022;
    private RadioButton radioButton157;
    private RadioButton radioButton158;
    private RadioButton radioButton159;
    private RadioButton radioButton161;
    private RadioButton radioButton160;

    private EditText editTextTextPersonName60;
    private EditText editTextTextPersonName61;
    private RadioGroupPlus radioGroupPlus024;
    private RadioButton radioButton05;
    private RadioButton radioButton06;
    private RadioGroupPlus radioGroupPlus025;
    private RadioButton radioButton07;
    private RadioButton radioButton08;
    private EditText editTextTextPersonName01;
    private EditText editTextTextPersonName03;
    private EditText editTextTextPersonName173;
    private EditText editTextTextPersonName174;

    private RadioGroupPlus radioGroupPlus037;
    private RadioButton radioButton167;
    private RadioButton radioButton168;
    private RadioButton radioButton169;
    private RadioButton radioButton170;

    private RadioGroupPlus radioGroupPlus038;
    private RadioButton radioButton210;
    private EditText editTextTextPersonName48;
    private EditText editTextTextPersonName167;
    private EditText editTextTextPersonName168;
    private EditText editTextTextPersonName175;

    private RadioGroupPlus radioGroupPlus07;
    private RadioButton radioButton011;
    private RadioButton radioButton12;
    private RadioButton radioButton536;
    private RadioButton radioButton012;
    private RadioButton radioButton537;
    private EditText editTextTextPersonName180;

    private RadioGroupPlus radioGroupPlus040;
    private RadioButton radioButton533;
    private RadioButton radioButton534;
    private EditText editTextTextPersonName133;
    private EditText editTextTextPersonName59;


    private TextView textView751;
    private TextView textView752;
    private EditText editTextTextPersonName163;
    private Button button31;
    private Button button32;

    private int mSex =1;
    private String mTel = "";
    private String mIdCard ="";
    private int mAge =1;
    private double mWeight=0.0;
    private double mHeight=0.0;
    private int eyesRight = 0;
    private int eyesLeft=0;
    private int mOral=0;
    private int mOentalCariesNumber=0;
    private double mHemoglobin=0;
    private int mPneumonia = 0;
    private int mDiarrhea = 0;
    private int mTrauma = 0;

    private FollowThreeToSixYearVo followThreeToSixYearVo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this)).get(GlobalViewModel.class);
        context = this;
        setContentView(R.layout.record_threetosix_health);
        initView();

    }

    private void initView() {
        textView177 = findViewById(R.id.textView177);
        textView177 = findViewById(R.id.textView177);
        textView179 = findViewById(R.id.textView179);
        textView183 = findViewById(R.id.textView183);
        textView184 = findViewById(R.id.textView184);
        textView368 = findViewById(R.id.textView368);

        editTextTextPersonName44 = findViewById(R.id.editTextTextPersonName44);
        radioGroupPlus021 = findViewById(R.id.radioGroupPlus021);
        radioButton129 = findViewById(R.id.radioButton129);
        radioButton130 = findViewById(R.id.radioButton130);
        radioButton132 = findViewById(R.id.radioButton132);
        editTextTextPersonName46 = findViewById(R.id.editTextTextPersonName46);
        radioGroupPlus020 = findViewById(R.id.radioGroupPlus020);
        radioButton207 = findViewById(R.id.radioButton207);
        radioButton208 = findViewById(R.id.radioButton208);
        radioButton209 = findViewById(R.id.radioButton209);

        radioGroupPlus022 = findViewById(R.id.radioGroupPlus022);
        radioButton157 = findViewById(R.id.radioButton157);
        radioButton158 = findViewById(R.id.radioButton158);
        radioButton159 = findViewById(R.id.radioButton159);
        radioButton161 = findViewById(R.id.radioButton161);
        radioButton160 = findViewById(R.id.radioButton160);

        editTextTextPersonName60 = findViewById(R.id.editTextTextPersonName60);
        editTextTextPersonName61 = findViewById(R.id.editTextTextPersonName61);
        radioGroupPlus024 = findViewById(R.id.radioGroupPlus024);
        radioButton05 = findViewById(R.id.radioButton05);
        radioButton06 = findViewById(R.id.radioButton06);
        radioGroupPlus025 = findViewById(R.id.radioGroupPlus025);
        radioButton07 = findViewById(R.id.radioButton07);
        radioButton08 = findViewById(R.id.radioButton08);
        editTextTextPersonName01 = findViewById(R.id.editTextTextPersonName01);
        editTextTextPersonName03 = findViewById(R.id.editTextTextPersonName03);
        editTextTextPersonName173 = findViewById(R.id.editTextTextPersonName173);
        editTextTextPersonName174 = findViewById(R.id.editTextTextPersonName174);

        radioGroupPlus037 = findViewById(R.id.radioGroupPlus037);
        radioButton167 = findViewById(R.id.radioButton167);
        radioButton168 = findViewById(R.id.radioButton168);
        radioButton169 = findViewById(R.id.radioButton169);
        radioButton170 = findViewById(R.id.radioButton170);

        radioGroupPlus038 = findViewById(R.id.radioGroupPlus038);
        radioButton210 = findViewById(R.id.radioButton210);
        editTextTextPersonName48 = findViewById(R.id.editTextTextPersonName48);
        editTextTextPersonName167 = findViewById(R.id.editTextTextPersonName167);
        editTextTextPersonName168 = findViewById(R.id.editTextTextPersonName168);
        editTextTextPersonName175 = findViewById(R.id.editTextTextPersonName175);

        radioGroupPlus07 = findViewById(R.id.radioGroupPlus07);
        radioButton011 = findViewById(R.id.radioButton011);
        radioButton12 = findViewById(R.id.radioButton12);
        radioButton536 = findViewById(R.id.radioButton536);
        radioButton012 = findViewById(R.id.radioButton012);
        radioButton537 = findViewById(R.id.radioButton537);
        editTextTextPersonName180 = findViewById(R.id.editTextTextPersonName180);

        radioGroupPlus040 = findViewById(R.id.radioGroupPlus040);
        radioButton533 = findViewById(R.id.radioButton533);
        radioButton534 = findViewById(R.id.radioButton534);
        editTextTextPersonName133 = findViewById(R.id.editTextTextPersonName133);
        editTextTextPersonName59 = findViewById(R.id.editTextTextPersonName59);


        textView751 = findViewById(R.id.textView751);
        textView752 = findViewById(R.id.textView752);
        editTextTextPersonName163 = findViewById(R.id.editTextTextPersonName163);
        button32 = findViewById(R.id.button31);
        button31 = findViewById(R.id.button32);


        button31.setOnClickListener(this); // back
        button32.setOnClickListener(this); // commit

//        // 性别选择
        textView179.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
                View view = View.inflate(ThreeToSixYearFollowActivity.this, R.layout.dialog_sex, null);
                alertDialog
                        .setView(view)
                        .create();
                final AlertDialog show = alertDialog.show();
                TextView tv_boy = view.findViewById(R.id.tv_boy);
                TextView tv_girl = view.findViewById(R.id.tv_girl);
                TextView btn_cancel = view.findViewById(R.id.btn_cancel);

                tv_boy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mSex = 1;
                        show.dismiss();
                    }
                });
                tv_girl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mSex = 2;
                        show.dismiss();
                    }
                });

                btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        show.dismiss();
                    }
                });


            }
        });


        textView751.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimePickerView pvTime = new TimePickerView.Builder(ThreeToSixYearFollowActivity.this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        textView751.setText(Utils.getTime(date));
                    }
                })
                        .setType(new boolean[]{true, true, true, false, false, false})
                        .setTitleText("随访时间")
                        .setTitleColor(Color.WHITE)//标题文字颜色
                        .setSubmitColor(Color.parseColor("#43c489"))
                        .setCancelColor(Color.parseColor("#43c489"))
                        .setTitleBgColor(Color.parseColor("#43c489"))
                        .setDividerColor(Color.parseColor("#43c489"))
                        .setTextColorCenter(Color.parseColor("#43c489"))
                        .setTitleSize(14)
                        .setContentSize(14)
                        .setSubCalSize(14)
                        .build();

                pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
                pvTime.show();
            }
        });
        textView752.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimePickerView pvTime = new TimePickerView.Builder(ThreeToSixYearFollowActivity.this, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        textView752.setText(Utils.getTime(date));
                    }
                })
                        .setType(new boolean[]{true, true, true, false, false, false})
                        .setTitleText("下次随访时间")
                        .setTitleColor(Color.WHITE)//标题文字颜色
                        .setSubmitColor(Color.parseColor("#43c489"))
                        .setCancelColor(Color.parseColor("#43c489"))
                        .setTitleBgColor(Color.parseColor("#43c489"))
                        .setDividerColor(Color.parseColor("#43c489"))
                        .setTextColorCenter(Color.parseColor("#43c489"))
                        .setTitleSize(14)
                        .setContentSize(14)
                        .setSubCalSize(14)
                        .build();

                pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
                pvTime.show();
            }
        });
    }
    /**
     * 保存数据
     */
    private void save() {

        //体重评估: (1 上 2 中 4 下)
        int checkedRadioButtonId = radioGroupPlus021.getCheckedRadioButtonId();
        String weightLevel = "1";
        switch (checkedRadioButtonId) {
            case R.id.radioButton129:
                weightLevel = "1";
                break;
            case R.id.radioButton130:
                weightLevel = "2";
                break;
            case R.id.radioButton132:
                weightLevel = "4";
                break;

        }


        //身长评估 (1 上 2 中 4 下)
        checkedRadioButtonId = radioGroupPlus020.getCheckedRadioButtonId();
        String heightLevel = "1";
        switch (checkedRadioButtonId) {
            case R.id.radioButton207:
                heightLevel = "1";
                break;
            case R.id.radioButton208:
                heightLevel = "2";
                break;
            case R.id.radioButton209:
                heightLevel = "4";
                break;
        }
        //体格发育评价   体格发育评价    1正常    2低体重    3消瘦    4生长迟缓   5超重
        checkedRadioButtonId = radioGroupPlus022.getCheckedRadioButtonId();
        String physicaldevelopAssessment = "1";
        switch (checkedRadioButtonId) {
            case R.id.radioButton157:
                physicaldevelopAssessment = "1";
                break;
            case R.id.radioButton158:
                physicaldevelopAssessment = "2";
                break;
            case R.id.radioButton159:
                physicaldevelopAssessment = "3";
                break;
            case R.id.radioButton161:
                physicaldevelopAssessment = "4";
                break;
            case R.id.radioButton160:
                physicaldevelopAssessment = "5";
                break;
        }

        //  胸部 1 未见异常2 异常
        checkedRadioButtonId = radioGroupPlus024.getCheckedRadioButtonId();
        String chest = "1";
        switch (checkedRadioButtonId){
            case R.id.radioButton05:
                chest = "1";
                break;
            case R.id.radioButton06:
                chest = "2";
                break;
        }


        //腹部 1 未见异常2 异常
        checkedRadioButtonId = radioGroupPlus025.getCheckedRadioButtonId();
        String abdomen = "";
        switch (checkedRadioButtonId){
            case R.id.radioButton07:
                abdomen = "1";
                break;
            case R.id.radioButton08:
                abdomen = "2";
                break;
        }

        // 发育评估
        checkedRadioButtonId = radioGroupPlus037.getCheckedRadioButtonId();
        String developmentAssessment = "";
        switch (checkedRadioButtonId){
            case R.id.radioButton167:
                developmentAssessment = "1";
                break;
            case R.id.radioButton168:
                developmentAssessment = "2";
                break;
            case R.id.radioButton169:
                developmentAssessment = "3";
                break;
            case R.id.radioButton170:
                developmentAssessment = "4";
                break;
        }


        // 转诊建议0 无1 有
        checkedRadioButtonId = radioGroupPlus040.getCheckedRadioButtonId();
        String referralProposal = "0";
        switch (checkedRadioButtonId){
            case R.id.radioButton533:
                referralProposal = "0";
                break;
            case R.id.radioButton534:
                referralProposal = "1";
                break;

        }

        followThreeToSixYearVo = new FollowThreeToSixYearVo();
        //病人id
        followThreeToSixYearVo.setInhabitantId(1291);
        //被随访病人姓名
        followThreeToSixYearVo.setInhabitantName(textView177.getText().toString());
        //月龄
        followThreeToSixYearVo.setMonth(6);
        //随访日期
        followThreeToSixYearVo.setFollowTime(textView751.getText().toString());
        // 体重：kg
        followThreeToSixYearVo.setWeight(Double.valueOf(editTextTextPersonName44.getText().toString()));
        // * 体重评估: 1上 2中 4下
        followThreeToSixYearVo.setWeightLevel(Integer.valueOf(weightLevel));
        //身长：cm
        followThreeToSixYearVo.setHeight(Double.valueOf(editTextTextPersonName46.getText().toString()));
        // * 身长评估: 1上 2中 4下
        followThreeToSixYearVo.setHeightLevel(Integer.valueOf(heightLevel));
        // 体格发育评价    * 1正常  * 2低体重* 3消瘦* 4生长迟缓* 5超重
        followThreeToSixYearVo.setPhysicaldevelopAssessment(Integer.valueOf(physicaldevelopAssessment));
        // 视力   * 1正常  * 2异常
        followThreeToSixYearVo.setEyesight(1);
        //视力值
        followThreeToSixYearVo.setEyesightValue(1.9);
        //胸部 1 未见异常   2 异常
        followThreeToSixYearVo.setChest(Integer.valueOf(chest));
        //腹部 1 未见异常   2 异常
        followThreeToSixYearVo.setAbdomen(Integer.valueOf(abdomen));
        //  口腔:出牙数量
        followThreeToSixYearVo.setOral(Integer.valueOf(editTextTextPersonName01.getText().toString()));
        //  龋齿数:出牙数量
        followThreeToSixYearVo.setDentalCariesNumber(Integer.valueOf(editTextTextPersonName03.getText().toString()));
        //血红蛋白值 g/l
        followThreeToSixYearVo.setHemoglobin(Double.valueOf(editTextTextPersonName173.getText().toString()));
        //发育评估:  1.不会说自己的名字  2.不会玩“拿棍当马骑”等假想游戏  3.不会模仿画圆 4.不会双脚跳
        followThreeToSixYearVo.setDevelopmentAssessment(Integer.valueOf(developmentAssessment));
        //肺炎次数
        followThreeToSixYearVo.setPneumonia(Integer.valueOf(editTextTextPersonName48.getText().toString()));
        //腹泻次数
        followThreeToSixYearVo.setDiarrhea(Integer.valueOf(editTextTextPersonName167.getText().toString()));
        //外伤次数
        followThreeToSixYearVo.setTrauma(Integer.valueOf(editTextTextPersonName168.getText().toString()));
        //* 是否患病 0否 1是
        followThreeToSixYearVo.setFollowUpPrevalence(0);
        //患病其他
        followThreeToSixYearVo.setPrevalenceOther(editTextTextPersonName175.getText().toString());
        //指导  1合理膳食 2生长发育 3疾病预防 4预防伤害 5口腔保健 6其他
        followThreeToSixYearVo.setGuide(1);
        // 其他指导
        followThreeToSixYearVo.setOtherGuide(editTextTextPersonName180.getText().toString());
        //转诊建议 0 无 1 有
        followThreeToSixYearVo.setReferralProposal(Integer.valueOf(referralProposal));
        //转诊原因
        followThreeToSixYearVo.setReferralReason(editTextTextPersonName59.getText().toString());
        //转诊机构
        followThreeToSixYearVo.setReferralOrg(editTextTextPersonName133.getText().toString());
        //下次随访时间
        followThreeToSixYearVo.setNextFollowTime(textView752.getText().toString());
        //医生id
        followThreeToSixYearVo.setDoctorId(1);
        // 随访医生姓名
        followThreeToSixYearVo.setDoctorName("mmm");
        //其他信息
        followThreeToSixYearVo.setOtherJson("dddddd");
        //是否被逻辑删除  0 否，未被删除  1 是，已被删除
        followThreeToSixYearVo.setIsDelete(0);
        //病人签名图片地址
        followThreeToSixYearVo.setInhSignName("jjjjjjjjj");
        //随访医生签名图片地址
        followThreeToSixYearVo.setDocSignName("ooooooo");

        RequestUtils.postJson(HISConfig.REMOTE_URL + "followThreeToSixYear/add",followThreeToSixYearVo, new AppCallBack(context) {
            @Override
            public void onResponse(String response, int id) {
                if (StringUtils.isEmpty(response)){
                    Toast.makeText(context,"同步的医生数据为空！",Toast.LENGTH_SHORT);
                    return;
                }

            }

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                String s = GsonUtils.toJson(followThreeToSixYearVo);
                Log.d("followThreeToSixYearVo:","====>>>"+s);
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.textView344:
//                finish();
//                break;
//            case R.id.textView1:
//                Utils.Toast(context, "该功能尚未开放！");
//                break;
            case R.id.button31:
                finish();
                break;
            case R.id.button32:
                save();
                break;
        }

    }


}